home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / PGPuam / In process / dumpuserInfo.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  20.5 KB  |  745 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h> /* needed for offsetof */
  4. #include <string.h>
  5. #include <console.h>
  6. #include <sioux.h>
  7. #include <PLStringFuncs.h>
  8.  
  9. #include "AppleShareRegistry.h"
  10. #include "AppleShareFileServerRegistry.h"
  11.  
  12.  
  13. #define PGP_MACINTOSH 1
  14.  
  15. #include "pgpErrors.h"
  16. #include "pgpKeys.h"
  17. #include "pgpMemoryMgr.h"
  18. #include "pgpUtilities.h"
  19.  
  20.  
  21. void BuildObjectSpecByShortID(OAMObjectSpec *obj, OAMShortObjectSpec id)
  22. {
  23.     memset(obj, 0, sizeof(OAMObjectSpec));
  24.     obj->specType = kOAMObjectSpecByShortID;
  25.     obj->u.shortID = id;
  26. }
  27.  
  28. void BuildObjectSpecByNameType(OAMObjectSpec *obj, StringPtr name, OAMType type)
  29. {
  30.     short len = 0;
  31.     
  32.     memset(obj, 0, sizeof(OAMObjectSpec));
  33.     obj->specType = kOAMObjectSpecByNameType;
  34.     obj->objectType = type;
  35.     len = *name + 1;
  36.     memcpy(obj->u.name, name, len);
  37. }
  38. void DisplayAttr(OAMAttributeDescriptor *attr)
  39. {
  40.     if (attr && attr->attributeSignature) {
  41.         printf("---------------------------------\n");
  42. //        printf("attr->status = %d\n",attr->status );
  43. //        printf("attr->offset = %d\n",attr->offset );
  44. //        printf("attr->actSize = %d\n",attr->actSize );
  45. //        printf("attr->bufferDescriptor.bufferLen = %d\n",attr->bufferDescriptor.bufferLen );
  46. //        printf("attr->bufferDescriptor.actCount = %d\n",attr->bufferDescriptor.actCount );
  47.         if (attr->status == 0)
  48.         {
  49.         switch (attr->attributeSignature) {
  50.             case kOAMBasic:
  51.                 switch (attr->attributeType) {
  52.                         case kOAMName:
  53.                         {
  54.                              StringPtr name = (StringPtr)attr->bufferDescriptor.buffer;
  55.                             p2cstr(name);
  56.                             printf("Name = %s",name);
  57.                         }
  58.                         break;
  59.                         case kOAMInternetName:
  60.                         {
  61.                             StringPtr name = (StringPtr)attr->bufferDescriptor.buffer;
  62.                             p2cstr(name);
  63.                             printf("Name = %s",name);
  64.                         }
  65.                         break;
  66.                         case kOAMShortID:
  67.                             printf("ID = %d",*((UInt32*)attr->bufferDescriptor.buffer));
  68.                         break;    
  69.                         case kOAMType:
  70.                             printf("Type = %.4s",(char*)attr->bufferDescriptor.buffer);
  71.                         break;    
  72.                 };
  73.             break;
  74.             case kOAMUser:
  75.                 switch (attr->attributeType) {
  76.                         case kOAMUserFlags:
  77.                             printf("userFlags = 0x%x",*((UInt16*)attr->bufferDescriptor.buffer));
  78.                         break;    
  79.                         case kOAMUserComment:
  80.                         {
  81.                             printf("comment = %#s",(StringPtr)attr->bufferDescriptor.buffer);
  82.                         }
  83.                         break;
  84.                         case kOAMPasswordLen:
  85.                             printf("pwdLen = %d",*((UInt8*)attr->bufferDescriptor.buffer));
  86.                         break;
  87.                         case kOAMLastLogin:
  88.                         {
  89.                             DateTimeRec    date = {};
  90.                             SecondsToDate(*(UInt32*)attr->bufferDescriptor.buffer, &date);
  91.                             printf("kLastLogin: %d/%d/%d", date.month, date.day, date.year);
  92.                         }
  93.                         break;
  94.                 };
  95.                 
  96.             break;
  97.             
  98.             case kOAMService:
  99.                 switch (attr->attributeType) {
  100.                         case kOAMShortStatus:
  101.                             printf("shortStatus = 0x%x",*((UInt32*)attr->bufferDescriptor.buffer));
  102.                         break;
  103.                         /*
  104.                         case kOAMDetailedStatus:
  105.                             printf("detailedStatus = 0x%x",*((UInt32*)attr->bufferDescriptor.buffer));
  106.                         break;    
  107.                         */
  108.                         case kOAMServiceFlags:
  109.                             printf("kServiceFlags = 0x%x",*((UInt32*)attr->bufferDescriptor.buffer));
  110.                         break;    
  111.                 };
  112.             break;
  113.             
  114.             case kOAMMachine:
  115.                 switch (attr->attributeType) {
  116.                         case kOAMGuestProgramLinking:
  117.                             printf("kGuestProgramLinking = 0x%x",*((UInt8*)attr->bufferDescriptor.buffer));
  118.                         break;    
  119.                         case kOAMNoGuestAccess:
  120.                             printf("kNoGuestAccess = 0x%x",*((UInt8*)attr->bufferDescriptor.buffer));
  121.                         break;    
  122.                         case kOAMProgramLinking:
  123.                             printf("kProgramLinking = 0x%x",*((UInt8*)attr->bufferDescriptor.buffer));
  124.                         break;    
  125.                         case kOAMServerName:
  126.                             printf("name = %#s",(StringPtr)attr->bufferDescriptor.buffer);
  127.                         break;    
  128.                         case kOAMMultihoming:
  129.                             printf("kMultihoming = 0x%x",*((UInt8*)attr->bufferDescriptor.buffer));
  130.                         break;    
  131.                         case kOAMNoSavePassword:
  132.                             printf("kNoSavePassword = 0x%x",*((UInt8*)attr->bufferDescriptor.buffer));
  133.                         break;    
  134.                         case kOAMMinPasswordLen:
  135.                             printf("kMinPasswordLen = 0x%x",*((UInt8*)attr->bufferDescriptor.buffer));
  136.                         break;    
  137.                         case kOAMMaxBadLogins:
  138.                             printf("kMaxBadLogins = %d",*((UInt16*)attr->bufferDescriptor.buffer));
  139.                         break;    
  140.                         case kOAMDefaultShutdown:
  141.                             printf("kDefaultShutdown = %d",*((UInt16*)attr->bufferDescriptor.buffer));
  142.                         break;    
  143.                         case kOAMUniqueID:
  144.                             printf("kUniqueID = %d",*((UInt32*)attr->bufferDescriptor.buffer));                        
  145.                         break;
  146.                 };
  147.             break;
  148.  
  149.             case kOAMGestalt:
  150.                 printf("selector: %.4s response: %X", (char*)&attr->attributeType, *((SInt32*)attr->bufferDescriptor.buffer));
  151.             break;            
  152.             
  153.  
  154.         }    
  155.         printf(" <status, len> = <%d, %d> ",attr->status,attr->bufferDescriptor.actCount );
  156.         printf("\n---------------------------------\n");
  157.         
  158.         }
  159.     
  160.     }
  161. }
  162.  
  163.  
  164. void MyListAllUserNames(OAMSessionID sess)
  165. {    
  166.     OAMStatus            err = noErr;
  167.     OAMIterationSpec    iter;
  168.     UInt32                maxToGet    = 15;
  169.     OAMType                typeList[2] = {kOAMUser,0};
  170.     char                bd_buffer[4096] = {};
  171.     char                iter_buffer[512] = {};
  172.     OAMBufferDescriptor    bd    = {};
  173.     OAMAttributeDescriptor iter_attr[2] = {};
  174.     Str31                userName = {};
  175.     int                 userCount = 0;
  176.     
  177.     /* set up buffer descriptor */
  178.     bd.buffer    = bd_buffer;
  179.     bd.bufferLen = sizeof(bd_buffer);
  180.     bd.actCount    = 0;
  181.     
  182.     /* set up iter spec */
  183.     memset(&iter, 0 , sizeof(iter));
  184.     iter.iterationType = kOAMIterObjects;
  185.     iter.typeList    = typeList;
  186.     iter.iterationFlags = kOAMIterByIndex;
  187.     iter.u.startingIndex = 1;
  188.     iter.maxToGet = maxToGet;
  189.  
  190.     /* set up iter_attr */
  191.     iter_attr[0].attributeSignature = kOAMBasic;
  192.      iter_attr[0].attributeType = kOAMName; 
  193.      iter_attr[0].bufferDescriptor.buffer = userName;
  194.     iter_attr[0].bufferDescriptor.bufferLen = sizeof(iter_buffer);
  195.     iter_attr[0].bufferDescriptor.actCount = 0;
  196.     /* null terminate array of attribute descriptors */
  197.  
  198.      iter_attr[1].attributeSignature = NULL;
  199.     
  200.     do
  201.     {
  202.         err = OAMIterate(sess, &iter, iter_attr, &bd, NULL);
  203.         
  204.         if(!err) {
  205.             OAMObjectSpec obj = {};
  206.             OAMParseInfo parseInfo;
  207.             int            attrIndex;
  208.             
  209.             memset(&parseInfo, 0, sizeof(OAMParseInfo));
  210.             err = OAMParseAttributeBuffer(&bd, iter_attr, &parseInfo);            
  211.          
  212.             while(OAMParseGetNextObject(&parseInfo, &obj) != kOAMParseDone) {
  213.                 printf("id = %d type = %s\n", obj.u.shortID, (char*)&obj.objectType);
  214.                 for ( attrIndex = 0; ; attrIndex++)
  215.                 {
  216.                     if (OAMParseGetNextAttribute(&parseInfo, &iter_attr[attrIndex]) != kOAMParseDone)
  217.                         DisplayAttr(&iter_attr[attrIndex]);
  218.                     else
  219.                         break;
  220.                 }
  221.                 userCount++;
  222.             }
  223.             iter.u.startingIndex += maxToGet;
  224.         }
  225.     } while (!err && iter.more );
  226.     printf("userCount(%d)\n",userCount);
  227. }
  228.  
  229.  
  230.  
  231.  
  232. OAMStatus MyGetMachineName(OAMSessionID sess, StringPtr machineName)
  233. {
  234.     OAMStatus        err = noErr;
  235.     OAMObjectSpec    machineObj;    
  236.     OAMAttributeDescriptor myAttrDesc[2] = {};
  237.     
  238.     memset(&myAttrDesc, 0, sizeof(myAttrDesc));
  239.     
  240.     myAttrDesc[0].attributeSignature = kOAMMachine;
  241.     myAttrDesc[0].attributeType = kOAMServerName;
  242.     myAttrDesc[0].bufferDescriptor.buffer = machineName;
  243.     myAttrDesc[0].bufferDescriptor.bufferLen = sizeof(Str31);
  244.     myAttrDesc[0].bufferDescriptor.actCount = sizeof(Str31);
  245.     /* null terminate array of attribute descriptors */
  246.     myAttrDesc[1].attributeSignature = NULL;
  247.  
  248.     BuildObjectSpecByShortID(&machineObj, kOAMMachineShortID);    
  249.     err = OAMGetAttribute(sess, &machineObj,  myAttrDesc, NULL);
  250.  
  251.     return err;
  252. }
  253.  
  254. void MyListRegistryAgents(unsigned char *  name, unsigned char * zone)
  255. {
  256.     UInt32                numInBuffer    = 0;
  257.     UInt32                numFound    = 0;
  258.     OAMStatus            err            = noErr;
  259.     char                buffer[2048];
  260.     OAMBufferDescriptor    lookup_bd;
  261.  
  262.  
  263.     OAMServerLocator*    loc            = NULL;
  264.     int                    loc_size    = offsetof(OAMServerLocator,protSpecific) + sizeof(Str32);
  265.  
  266.     OAMServerSpec        serverSpec;    
  267.     
  268.     int                    serverIndex;
  269.     
  270.     /* set up buffer descriptor */
  271.     lookup_bd.buffer    = buffer;
  272.     lookup_bd.bufferLen    = sizeof(buffer);
  273.     lookup_bd.actCount    = 0;
  274.     
  275.     /* Build AppleTalk OAMServerLocator */
  276.     loc = (OAMServerLocator *) NewPtr(loc_size);
  277.     memset(loc, 0, loc_size);
  278.     loc->specSize = loc_size;
  279.     loc->protType = kOAMLocatorAppletalk;
  280.     memcpy (loc->name, name, sizeof(Str32));            
  281.     memcpy (loc->protSpecific, zone, sizeof(Str32));    
  282.     
  283.      err = OAMFindServer(loc, &lookup_bd, &numInBuffer, &numFound, NULL);
  284.     printf("Found %d AppleShare Registry Agent(s).\n", numFound);    
  285.  
  286.     for (serverIndex = 1; serverIndex<=numInBuffer; serverIndex++)
  287.     {
  288.         err = OAMFindServerExtract(&lookup_bd, serverIndex,&serverSpec);
  289.         p2cstr(serverSpec.name);
  290.         printf("%s\n", serverSpec.name);
  291.     }
  292. }
  293.  
  294.  OAMSessionID MyOpenSession(Str31 serverName, Str31 zoneName)
  295. {
  296.     OAMStatus         err = noErr;
  297.     OAMSessionID     sessID = 0;
  298.     UInt32             numInBuffer = 0;
  299.     UInt32             numFound = 0;
  300.     OAMServerLocator *loc;
  301.     OAMServerSpec     server;
  302.     UInt32             loc_size= offsetof(OAMServerLocator,protSpecific) + sizeof(Str32);
  303.  
  304.     /* set up the server locator */
  305.     loc = (OAMServerLocator *)new char [loc_size];
  306.  
  307.     /* build the AppleTalk OAMServerLocator */
  308.     memset(loc, 0, loc_size);
  309.     loc->specSize = loc_size;
  310.     loc->protType = kOAMLocatorAppletalk;
  311.     memcpy (loc->name, serverName, sizeof(Str32));
  312.     memcpy (loc->protSpecific, zoneName, sizeof(Str32));
  313.  
  314.     /* set up buffer descriptor */
  315.     char buffer[512]= {};
  316.     OAMBufferDescriptor    lookup_bd  = { buffer, sizeof(buffer), 0};
  317.  
  318.     /* set up the server spec */
  319.     memset(&server, 0, sizeof(OAMServerSpec));
  320.     
  321.      err = OAMFindServer(loc, &lookup_bd, &numInBuffer, &numFound, NULL);
  322.      if(!err && numFound != 0) 
  323.     {
  324.         err = OAMFindServerExtract(&lookup_bd, 1, &server);
  325.         err = OAMOpenSession(&server, &sessID, NULL);
  326.         printf("OAMOpenSession: %d \n", err);
  327.         if (err == noErr) 
  328.         {
  329.             printf("sessionID = %d\n", sessID);
  330.         }
  331.     } else
  332.         printf("Server Not Found\n");
  333.  
  334.     return sessID;
  335. }
  336.  
  337. OAMStatus MyAuthenticate(OAMSessionID sess, StringPtr name, StringPtr password)
  338. {    
  339.     OAMStatus        err = noErr;
  340.     OAMObjectSpec    obj;
  341.     OAMKey            key;
  342.     
  343.     memset(&key, 0, sizeof(key));
  344.     key.keyBufferLen = 8;
  345.  
  346.      memcpy(&key.keyBuffer, &password[1], password[0]);
  347.     
  348.     BuildObjectSpecByNameType(&obj, name, kOAMUser);
  349.     
  350.     err = OAMAuthenticateSession(sess, &obj, &key, NULL);
  351.  
  352.     return err;
  353. }
  354.  
  355. OAMStatus RemoveServerKey(OAMSessionID sessionId)
  356. {
  357.  
  358.     OAMStatus                err = noErr;
  359.     OAMObjectSpec            obj;    
  360.     OAMAttributeDescriptor     attr[2] = {};
  361.     
  362.     unsigned char            key[4096];
  363.         
  364.      memset(&attr, 0, sizeof(attr));
  365.  
  366.     attr[0].attributeSignature             = kOAMMachine;
  367.     attr[0].attributeType                 = 'PGPs';
  368.     attr[0].bufferDescriptor.buffer     =  key;
  369.     attr[0].bufferDescriptor.bufferLen     =  sizeof(key);
  370.       attr[1].attributeSignature = NULL;
  371.  
  372.     memset(&obj, 0, sizeof(OAMObjectSpec));
  373.     
  374.     obj.specType = kOAMObjectSpecByShortID;
  375.     obj.u.shortID = kOAMMachineShortID;
  376.   
  377.       err = OAMDeleteAttribute(sessionId, &obj,  attr, NULL);
  378.       return err;
  379.       
  380. }
  381.  
  382. PGPError GetKeyMaterial(PGPContextRef context, PGPKeySetRef keyset , StringPtr keyName, Boolean privateKey, void** keyBuffer, PGPSize*  keyBufferLen )
  383. {
  384.     PGPFilterRef    theFilter         = kInvalidPGPFilterRef;
  385.     PGPKeyIterRef    theIterator     = kInvalidPGPKeyIterRef;
  386.     PGPKeyListRef    theKeyListRef     = kInvalidPGPKeyListRef;
  387.     PGPKeyRef        aKey             = kInvalidPGPKeyRef;
  388.     PGPKeySetRef    newKeySet         = NULL;
  389.     PGPUInt32          numKeys;
  390.     Boolean         canSign;
  391.     char            name[255];
  392.     PGPError        err;
  393.     
  394.     *keyBuffer = nil;
  395.     
  396. // Find key in database
  397.     PLstrcpy((StringPtr)name, keyName);
  398.     p2cstr((StringPtr)  name);
  399.     
  400.     err =   PGPNewUserIDStringFilter(context, name, kPGPMatchSubString, &theFilter);
  401.      err =     PGPFilterKeySet(keyset , theFilter, &newKeySet);
  402.      err =     PGPOrderKeySet(newKeySet,kPGPAnyOrdering, &theKeyListRef);
  403.     err =     PGPNewKeyIter( theKeyListRef, &theIterator );
  404.     
  405.     PGPCountKeys(newKeySet, &numKeys);
  406.  
  407.     if(numKeys > 0) 
  408.         while(IsntPGPError (PGPKeyIterNext( theIterator, &aKey )))
  409.          {
  410.              PGPGetKeyBoolean (aKey, kPGPKeyPropCanSign,  &canSign);
  411.  //            if(canSign)
  412.              {
  413.                  PGPKeySetRef    keys         = NULL;
  414.                   PGPKeyListRef    listRef         = kInvalidPGPKeyListRef;
  415.                   PGPKeyIterRef    iter             = kInvalidPGPKeyIterRef;
  416.                 PGPSubKeyRef    theSubKeyRef    = kInvalidPGPSubKeyRef;
  417.                 PGPUserIDRef    theUserIDRef    = kInvalidPGPUserIDRef;
  418.                 PGPSigRef        theSigRef        = kInvalidPGPSigRef;
  419.  
  420.                    PGPNewSingletonKeySet(aKey, &keys);
  421.        
  422.               // strip new keyset of sigs and subkeys...
  423.                    err =     PGPOrderKeySet(keys, kPGPAnyOrdering, &listRef);
  424.                    err =     PGPNewKeyIter( listRef, &iter );
  425.  
  426.                   while(IsntPGPError (PGPKeyIterNext( iter, &aKey )))
  427.                    {
  428.                        PGPUserIDRef primaryUserIDRef = kInvalidPGPUserIDRef;
  429.                         
  430.                        PGPGetPrimaryUserID(aKey, &primaryUserIDRef);
  431.  
  432.                      while( IsntPGPError(err = PGPKeyIterNextSubKey(iter, &theSubKeyRef )) && ( theSubKeyRef != NULL) )
  433.                     {
  434.                         
  435.                         err = PGPRemoveSubKey(theSubKeyRef);
  436.                       }
  437.         
  438.                       while( IsntPGPError(err = PGPKeyIterNextUserID(iter, &theUserIDRef )) && ( theUserIDRef != NULL) )
  439.                       {
  440.                         while( IsntPGPError(err = PGPKeyIterNextUIDSig(iter, &theSigRef )) && ( theSigRef != NULL) )
  441.                         {
  442.                             err = PGPRemoveSig(theSigRef);
  443.                           }
  444.                       
  445.                           if(theUserIDRef != primaryUserIDRef)  PGPRemoveUserID(theUserIDRef);
  446.                       }
  447.                       
  448.  
  449.                  }
  450.                  err = PGPExportKeySet( keys,
  451.                                     PGPOAllocatedOutputBuffer    (context, keyBuffer, MAX_PGPUInt32, keyBufferLen ),
  452.                                     PGPOArmorOutput    (context, false),
  453.                                     PGPOExportPrivateKeys(context, privateKey), // fix this
  454.                                     PGPOExportFormat(context,  kPGPExportFormat_Basic),
  455.                                     PGPOLastOption(context));
  456.                 printf("KeySize %d Bytes\n", *keyBufferLen);
  457.                  PGPFreeKeySet(keys);
  458.                   PGPFreeKeyIter(iter );
  459.                 PGPFreeKeyList(listRef );
  460.                   break;
  461.               }    
  462.           } 
  463.  
  464.     PGPFreeFilter(theFilter);
  465.     PGPFreeKeyIter(theIterator );
  466.     PGPFreeKeyList(theKeyListRef );
  467.      
  468.     return err;
  469. }
  470.  
  471. OAMStatus SetServerKey(OAMSessionID sessionId, void* key, UInt32 len)
  472. {
  473.     OAMStatus                err = noErr;
  474.     OAMObjectSpec            obj;    
  475.     OAMAttributeDescriptor     attr[2] = {};
  476.        
  477.      memset(&attr, 0, sizeof(attr));
  478.  
  479.     attr[0].attributeSignature             = kOAMMachine;
  480.     attr[0].attributeType                 = 'PGPs';
  481.     attr[0].bufferDescriptor.buffer     =  key;
  482.     attr[0].bufferDescriptor.bufferLen     =  len;
  483.       attr[1].attributeSignature = NULL;
  484.  
  485.     BuildObjectSpecByShortID(&obj,     kOAMMachineShortID);    
  486.   
  487.       err = OAMSetAttribute(sessionId, &obj,  attr, NULL);
  488.     
  489.     printf("Server Key set (%d, %d) (%d bytes)\n",    err, attr[0].status, attr[0].bufferDescriptor.bufferLen);
  490.  
  491.      if(err == noErr)
  492.      {
  493.  
  494.         if( attr[0].status == noErr)
  495.          {
  496.              printf(" Success\n");
  497.          }
  498.  
  499.     }
  500.  
  501.     return err;
  502. }
  503.  
  504. OAMStatus GetServerKey(PGPContextRef context, OAMSessionID sessionId)
  505. {
  506.  
  507.     OAMStatus                err = noErr;
  508.     OAMObjectSpec            obj;    
  509.     OAMAttributeDescriptor     attr[2] = {};
  510.     
  511.     unsigned char            key[4096];
  512.         
  513.      memset(&attr, 0, sizeof(attr));
  514.  
  515.     attr[0].attributeSignature             = kOAMMachine;
  516.     attr[0].attributeType                 = 'PGPs';
  517.     attr[0].bufferDescriptor.buffer     =  key;
  518.     attr[0].bufferDescriptor.bufferLen     =  sizeof(key);
  519.       attr[1].attributeSignature = NULL;
  520.  
  521.     memset(&obj, 0, sizeof(OAMObjectSpec));
  522.     
  523.     obj.specType = kOAMObjectSpecByShortID;
  524.     obj.u.shortID = kOAMMachineShortID;
  525.   
  526.       err = OAMGetAttribute(sessionId, &obj,  attr, NULL);
  527.  
  528.     if(err == noErr)
  529.      {
  530.  
  531.         if( attr[0].status == noErr)
  532.          {
  533.               PGPKeySetRef    newKeySet         = NULL;
  534.             PGPUInt32         numKeys;
  535.             PGPKeyListRef    theKeyListRef     = kInvalidPGPKeyListRef;
  536.             PGPKeyIterRef    theIterator     = kInvalidPGPKeyIterRef;
  537.             PGPKeyRef        aKey             = kInvalidPGPKeyRef;
  538.  
  539.              printf(" GetServer Key,  %d Bytes\n", attr[0].bufferDescriptor.actCount);
  540.               
  541.              err = PGPImportKeySet(context, &newKeySet, 
  542.                          PGPOInputBuffer( context, attr[0].bufferDescriptor.buffer, attr[0].bufferDescriptor.actCount ),
  543.                         PGPOLastOption( context) );
  544.             
  545.             PGPCountKeys(newKeySet, &numKeys);
  546.             if(numKeys > 0) 
  547.             {
  548.                 Boolean canSign;
  549.  
  550.                 PGPOrderKeySet(newKeySet,kPGPAnyOrdering, &theKeyListRef);
  551.                 PGPNewKeyIter( theKeyListRef, &theIterator);
  552.                 
  553.                 while(IsntPGPError (PGPKeyIterNext( theIterator, &aKey )))
  554.                 {
  555.                     PGPGetKeyBoolean (aKey, kPGPKeyPropCanSign,  &canSign);
  556.                     
  557.                     if(canSign)
  558.                         {
  559.                             char primaryUsernamebuf[256];
  560.  
  561.                             PGPGetPrimaryUserIDNameBuffer(aKey, sizeof primaryUsernamebuf,primaryUsernamebuf, NULL );
  562.                             printf("KeyName : \"%s\"\n", primaryUsernamebuf);
  563.                              break;
  564.                         }
  565.                 }
  566.  
  567.                  PGPFreeKeyIter(theIterator );
  568.                 PGPFreeKeyList(theKeyListRef );
  569.                 if(PGPKeySetRefIsValid (newKeySet)) PGPFreeKeySet(newKeySet);
  570.               }
  571.  
  572.         }
  573.     }
  574.  
  575.     return err;
  576.  }
  577.  
  578.  
  579. OAMStatus GetUserKey(PGPContextRef context, OAMSessionID sessionId, unsigned char* userName )
  580. {
  581.     OAMStatus                err = noErr;
  582.     OAMObjectSpec            obj;    
  583.     OAMAttributeDescriptor     attr[2] = {};
  584.     
  585.     unsigned char            key[4096];
  586.         
  587.      memset(&attr, 0, sizeof(attr));
  588.  
  589.     attr[0].attributeSignature             = kOAMUser;
  590.     attr[0].attributeType                 = 'PGPs';
  591.     attr[0].bufferDescriptor.buffer     =  key;
  592.     attr[0].bufferDescriptor.bufferLen     =  sizeof(key);
  593.       attr[1].attributeSignature = NULL;
  594.  
  595.      BuildObjectSpecByNameType(&obj,(StringPtr) userName, kOAMUser);
  596.  
  597.       err = OAMGetAttribute(sessionId, &obj,  attr, NULL);
  598.  
  599.     if (err == noErr)
  600.      {
  601.  
  602.         if( attr[0].status == noErr)
  603.          {
  604.               PGPKeySetRef    newKeySet         = NULL;
  605.             PGPUInt32         numKeys;
  606.             PGPKeyListRef    theKeyListRef     = kInvalidPGPKeyListRef;
  607.             PGPKeyIterRef    theIterator     = kInvalidPGPKeyIterRef;
  608.             PGPKeyRef        aKey             = kInvalidPGPKeyRef;
  609.  
  610.              printf(" GetUser Key,  %d Bytes\n", attr[0].bufferDescriptor.actCount);
  611.               
  612.              err = PGPImportKeySet(context, &newKeySet, 
  613.                          PGPOInputBuffer( context, attr[0].bufferDescriptor.buffer, attr[0].bufferDescriptor.actCount ),
  614.                         PGPOLastOption( context) );
  615.             
  616.             PGPCountKeys(newKeySet, &numKeys);
  617.             if(numKeys > 0) 
  618.             {
  619.                 Boolean canSign;
  620.  
  621.                 PGPOrderKeySet(newKeySet,kPGPAnyOrdering, &theKeyListRef);
  622.                 PGPNewKeyIter( theKeyListRef, &theIterator);
  623.                 
  624.                 while(IsntPGPError (PGPKeyIterNext( theIterator, &aKey )))
  625.                 {
  626.                     PGPGetKeyBoolean (aKey, kPGPKeyPropCanSign,  &canSign);
  627.                     
  628.     //                if(canSign)
  629.                         {
  630.                             char primaryUsernamebuf[256];
  631.  
  632.                             PGPGetPrimaryUserIDNameBuffer(aKey, sizeof primaryUsernamebuf,primaryUsernamebuf, NULL );
  633.                             printf("KeyName : \"%s\"\n", primaryUsernamebuf);
  634.                              break;
  635.                         }
  636.                 }
  637.  
  638.                  PGPFreeKeyIter(theIterator );
  639.                 PGPFreeKeyList(theKeyListRef );
  640.                 if(PGPKeySetRefIsValid (newKeySet)) PGPFreeKeySet(newKeySet);
  641.               }
  642.  
  643.         }
  644.     }
  645.      return err;
  646.  }
  647.  
  648.  
  649. OAMStatus SetUserKey(OAMSessionID sessionId, unsigned char* userName, void* key, UInt32 len )
  650. {
  651.     OAMStatus                err            = noErr;
  652.     OAMObjectSpec            obj        = {};
  653.     OAMAttributeDescriptor  attr[2] = {};
  654.  
  655.  
  656.      memset(&attr, 0, sizeof(attr));
  657.      
  658.     attr[0].attributeSignature             = kOAMUser;
  659.     attr[0].attributeType                 = 'PGPs';
  660.     attr[0].bufferDescriptor.buffer     =  key;
  661.     attr[0].bufferDescriptor.bufferLen     =  len;
  662.      attr[1].attributeSignature = NULL;
  663.  
  664.      BuildObjectSpecByNameType(&obj,(StringPtr) userName, kOAMUser);
  665.  
  666.     err = OAMSetAttribute(sessionId, &obj,  attr, NULL);
  667.     printf("User Key set (%d, %d) (%d bytes)\n",    err, attr[0].status, attr[0].bufferDescriptor.bufferLen);
  668.  
  669.      return err;
  670.  }
  671.  
  672.  
  673. int main ( void)
  674. {
  675.  
  676.     PGPContextRef    theContextRef    = kInvalidPGPContextRef;
  677.     PGPKeySetRef     theKeySetRef    = kInvalidPGPKeySetRef;
  678.     void             *keyBuffer = nil;
  679.     PGPSize            keyBufferLen = 0;
  680.  
  681.      OAMStatus         err = noErr;
  682.     OAMSessionID    sessionID = 0;
  683.      Str31            machineName = {};
  684.  
  685.  #if __MWERKS__
  686.     extern tSIOUXSettings    SIOUXSettings;
  687.     SIOUXSettings.asktosaveonclose = false;
  688.     SIOUXSettings.autocloseonquit    = false;
  689.     SIOUXSettings.rows = 40;
  690.     
  691. #endif
  692.  
  693.      err = PGPNewContext( kPGPsdkAPIVersion, &theContextRef );
  694.      err = PGPOpenDefaultKeyRings(theContextRef,   kPGPKeyRingOpenFlags_Mutable /* FALSE*/, &theKeySetRef);
  695.  
  696.       err = OAMInitialize(4, 0, NULL, NULL);
  697.       
  698. //     MyListRegistryAgents("\p≈", "\p*"); 
  699. //    MyListRegistryAgents("\pThe President's Humidor", "\p*"); 
  700. //    MyListRegistryAgents("\p≈" ,"\pDA3 4th - North" );
  701.  
  702. //    sessionID = MyOpenSession("\pPhlex Wannabe" ,"\pDA3 4th - North");
  703.        sessionID = MyOpenSession("\pThe President's Humidor", "\p*");
  704.  //    err = OAMOpenSession(NULL, &sessionID, NULL);
  705.  
  706.      if (sessionID)
  707.     {
  708.       err = MyAuthenticate(sessionID, "\pvinnie", "\pbunbun");
  709.       printf("Authenticate = %d\n",err);
  710.       
  711.         err = MyGetMachineName(sessionID, machineName);
  712.         printf("%d = MachineName(%#s) \n",err,  machineName);
  713.  
  714.          MyListAllUserNames(sessionID);
  715. /*         err = RemoveServerKey( sessionID);
  716.           printf("RemoveServerKey = %d\n",err);
  717.  
  718.          err = GetKeyMaterial(theContextRef, theKeySetRef, machineName, true, &keyBuffer, &keyBufferLen);
  719.          if(keyBuffer)
  720.         {
  721.               err = SetServerKey(sessionID, keyBuffer, keyBufferLen );
  722.               PGPFreeData(keyBuffer);    
  723.          }
  724.  
  725.            err = GetKeyMaterial(theContextRef, theKeySetRef, "\pVinnie Moscaritolo", false, &keyBuffer, &keyBufferLen);
  726.          if(keyBuffer)
  727.         {
  728.             err = SetUserKey(sessionID, "\pVinnie Moscaritolo",keyBuffer, keyBufferLen );
  729.                PGPFreeData(keyBuffer);    
  730.          }
  731.           
  732.         err = GetServerKey(theContextRef, sessionID);
  733.         err = GetUserKey(theContextRef, sessionID,"\pVinnie Moscaritolo");
  734.         err = GetUserKey(theContextRef, sessionID,"\pLeland Wallace");
  735.  
  736. */        
  737.         OAMCloseSession(sessionID, NULL);
  738.     }
  739.     err = OAMDeInitialize();
  740.  
  741.       if( PGPKeySetRefIsValid( theKeySetRef))     PGPFreeKeySet (theKeySetRef );
  742.      if( PGPContextRefIsValid( theContextRef))     PGPFreeContext(theContextRef );
  743.  
  744.      return 0;
  745.   }